﻿namespace WMS6.Web.Analytics
{
    using System;
    using System.Data;
    using System.Drawing;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.AnalyticsReportServiceRef;
    using WMS6.Web.UserControls;

    public class StockRptList : BasePage
    {
        protected Button btnExport;
        protected Button btnList;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F15241;
        protected GridView gvStock;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected Label lblMsg;
        protected Label lblReceiptInquiryList;
        protected Label lblTotalAllocated;
        protected Label lblTotalAllocatedQty;
        protected Label lblTotalCanuse;
        protected Label lblTotalCanuseQty;
        protected Label lblTotalCube;
        protected Label lblTotalCubeText;
        protected Label lblTotalGrossWGT;
        protected Label lblTotalGrossWGTText;
        protected Label lblTotalPicked;
        protected Label lblTotalPickedQty;
        protected Label lblTotalQty;
        protected Label lblTotalQtyText;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        private decimal TotalAllocatedQty;
        private decimal TotalCanuseQty;
        private decimal TotalCube;
        private decimal TotalGrossWGT;
        private decimal TotalPickedQty;
        private decimal TotalQty;
        protected UpdatePanel up1;

        public void BindData2StockList(StockRptQueryEntity stockRptQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(stockRptQuery.CurrentPage);
            DataSet ds = AnalyticsReportServiceClientProxy.GetInstance().GetStockRptInfo(stockRptQuery);
            base.BindData2GridView(this.gvStock, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            StockRptQueryEntity stockRptQuery = this.GetStockRptQueryEntity();
            stockRptQuery.IsGetAll = true;
            this.hdnActionID.Value = "EXPORT";
            string currentPageIndex = stockRptQuery.CurrentPage;
            stockRptQuery.CurrentPage = "0";
            try
            {
                using (DataSet ds = AnalyticsReportServiceClientProxy.GetInstance().GetStockRptInfo(stockRptQuery))
                {
                    base.Export2Excel("StockRpt.xls", "15241", this.gvStock, ds);
                }
            }
            finally
            {
                stockRptQuery.IsGetAll = false;
                stockRptQuery.CurrentPage = currentPageIndex;
                this.SetStockRptQueryEntity(stockRptQuery);
            }
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            StockRptQueryEntity stockRptQuery = this.GetStockRptQueryEntity();
            stockRptQuery.CurrentPage = "0";
            this.BindData2StockList(stockRptQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            StockRptQueryEntity stockRptQuery = this.GetStockRptQueryEntity();
            stockRptQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2StockList(stockRptQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            StockRptQueryEntity stockRptQuery = this.GetStockRptQueryEntity();
            stockRptQuery.OWNER_ID = "";
            stockRptQuery.SKU_ID = "";
            stockRptQuery.SKU_CLASS_ID = "";
            stockRptQuery.EXTERNAL_LOT = "";
            stockRptQuery.LOT_ATTR01 = "";
            stockRptQuery.LOT_ATTR02 = "";
            stockRptQuery.LOT_ATTR03 = "";
            stockRptQuery.LOT_ATTR04 = "";
            stockRptQuery.LOT_ATTR05 = "";
            stockRptQuery.LOT_ATTR06 = "";
            stockRptQuery.LOT_ATTR07 = "";
            stockRptQuery.LOT_ATTR08 = "";
            stockRptQuery.LOT_ATTR09 = "";
            stockRptQuery.LOT_ATTR10 = "";
            stockRptQuery.EXPIRY_DATE = "";
            stockRptQuery.RECEIPT_DATE = "";
            stockRptQuery.PRODUCE_DATE = "";
            stockRptQuery.EXTERNAL_LOT = "";
            stockRptQuery.ISHISTORY = "";
            this.BindData2StockList(stockRptQuery);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            StockRptQueryEntity stockRptQuery = this.GetStockRptQueryEntity();
            if (int.Parse(stockRptQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                stockRptQuery.CurrentPage = (int.Parse(stockRptQuery.CurrentPage) + 1).ToString();
                this.BindData2StockList(stockRptQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            StockRptQueryEntity stockRptQuery = this.GetStockRptQueryEntity();
            if (int.Parse(stockRptQuery.CurrentPage) > 0)
            {
                stockRptQuery.CurrentPage = (int.Parse(stockRptQuery.CurrentPage) - 1).ToString();
                this.BindData2StockList(stockRptQuery);
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            StockRptQueryEntity stockRptQuery = this.GetStockRptQueryEntity();
            this.BindData2StockList(stockRptQuery);
        }

        private StockRptQueryEntity GetStockRptQueryEntity()
        {
            return (base.PageQueryEntitySession as StockRptQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                StockRptQueryEntity stockRptQuery = this.GetStockRptQueryEntity();
                stockRptQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2StockList(stockRptQuery);
            }
        }

        protected void gvStock_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F15241", this.gvStock);
        }

        protected void gvStock_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                if (this.hdnActionID.Value != "EXPORT")
                {
                    e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                    e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                    ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                    e.Row.Attributes.Add("ondblclick", "JavaScript:document.getElementById('" + ((CheckBox) e.Row.FindControl("chbSelect")).ClientID + "').click();");
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
                    if (((Label) e.Row.FindControl("lblLotAttr09")) != null)
                    {
                        if (((Label) e.Row.FindControl("lblLotAttr09")).Text == "坏货")
                        {
                            ((Label) e.Row.FindControl("lblLotAttr09")).BackColor = Color.Red;
                        }
                        if (((Label) e.Row.FindControl("lblLotAttr09")).Text == "搁置")
                        {
                            ((Label) e.Row.FindControl("lblLotAttr09")).BackColor = Color.Yellow;
                        }
                    }
                    if (((Label) e.Row.FindControl("lblQty")) != null)
                    {
                        this.TotalQty += decimal.Parse((((Label) e.Row.FindControl("lblQty")).Text != "") ? ((Label) e.Row.FindControl("lblQty")).Text : "0");
                    }
                    if (((Label) e.Row.FindControl("lblAllocatedQty")) != null)
                    {
                        this.TotalAllocatedQty += decimal.Parse((((Label) e.Row.FindControl("lblAllocatedQty")).Text != "") ? ((Label) e.Row.FindControl("lblAllocatedQty")).Text : "0");
                    }
                    if (((Label) e.Row.FindControl("lblCanuseQty")) != null)
                    {
                        this.TotalCanuseQty += decimal.Parse((((Label) e.Row.FindControl("lblCanuseQty")).Text != "") ? ((Label) e.Row.FindControl("lblCanuseQty")).Text : "0");
                    }
                    if (((Label) e.Row.FindControl("lblPickedQty")) != null)
                    {
                        this.TotalPickedQty += decimal.Parse((((Label) e.Row.FindControl("lblPickedQty")).Text != "") ? ((Label) e.Row.FindControl("lblPickedQty")).Text : "0");
                    }
                    if (((Label) e.Row.FindControl("lblGrossWgt")) != null)
                    {
                        this.TotalGrossWGT += decimal.Parse((((Label) e.Row.FindControl("lblGrossWgt")).Text != "") ? ((Label) e.Row.FindControl("lblGrossWgt")).Text : "0");
                    }
                    if (((Label) e.Row.FindControl("lblCube")) != null)
                    {
                        this.TotalCube += decimal.Parse((((Label) e.Row.FindControl("lblCube")).Text != "") ? ((Label) e.Row.FindControl("lblCube")).Text : "0");
                    }
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                this.lblTotalQtyText.Text = base.GetPreferenceDigit(this.TotalQty);
                this.lblTotalAllocatedQty.Text = base.GetPreferenceDigit(this.TotalAllocatedQty);
                this.lblTotalPickedQty.Text = base.GetPreferenceDigit(this.TotalPickedQty);
                this.lblTotalCanuseQty.Text = base.GetPreferenceDigit(this.TotalCanuseQty);
                this.lblTotalGrossWGTText.Text = base.GetPreferenceDigit(this.TotalGrossWGT);
                this.lblTotalCubeText.Text = base.GetPreferenceDigit(this.TotalCube);
                if (this.hdnActionID.Value == "EXPORT")
                {
                    for (int i = 0; i < this.gvStock.Columns.Count; i++)
                    {
                        if (this.gvStock.Columns[i].Visible)
                        {
                            this.gvStock.Columns[i].ItemStyle.Reset();
                            this.gvStock.Columns[i].ControlStyle.Reset();
                        }
                    }
                }
            }
        }

        protected void gvStock_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                StockRptQueryEntity stockRptQuery = this.GetStockRptQueryEntity();
                stockRptQuery.SortField = e.SortExpression;
                if (stockRptQuery.SortDirection == "ASC")
                {
                    stockRptQuery.SortDirection = "DESC";
                }
                else if (stockRptQuery.SortDirection == "DESC")
                {
                    stockRptQuery.SortDirection = "ASC";
                }
                this.BindData2StockList(stockRptQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            StockRptQueryEntity stockRptQuery = new StockRptQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    stockRptQuery.OWNER_ID = this.PreviousPage.OwnerID;
                    stockRptQuery.SKU_ID = this.PreviousPage.SkuID;
                    stockRptQuery.SKU_CLASS_ID = this.PreviousPage.SkuClassID;
                    stockRptQuery.DESCR = this.PreviousPage.Descr;
                    stockRptQuery.VENDOR_ID = this.PreviousPage.VendorID;
                    stockRptQuery.LOT_ATTR01 = this.PreviousPage.LotAttribute01;
                    stockRptQuery.LOT_ATTR02 = this.PreviousPage.LotAttribute02;
                    stockRptQuery.LOT_ATTR03 = this.PreviousPage.LotAttribute03;
                    stockRptQuery.LOT_ATTR04 = this.PreviousPage.LotAttribute04;
                    stockRptQuery.LOT_ATTR05 = this.PreviousPage.LotAttribute05;
                    stockRptQuery.LOT_ATTR06 = this.PreviousPage.LotAttribute06;
                    stockRptQuery.LOT_ATTR07 = this.PreviousPage.LotAttribute07;
                    stockRptQuery.LOT_ATTR08 = this.PreviousPage.LotAttribute08;
                    stockRptQuery.LOT_ATTR09 = this.PreviousPage.LotAttribute09;
                    stockRptQuery.LOT_ATTR10 = this.PreviousPage.LotAttribute10;
                    stockRptQuery.EXPIRY_DATE = this.PreviousPage.ExpiryDate;
                    stockRptQuery.PRODUCE_DATE = this.PreviousPage.ProduceDate;
                    stockRptQuery.RECEIPT_DATE = this.PreviousPage.ReceiptDate;
                    stockRptQuery.EXTERNAL_LOT = this.PreviousPage.ExternalLot;
                    stockRptQuery.LOT = this.PreviousPage.Lot;
                    stockRptQuery.LOC_ID = this.PreviousPage.LocationID;
                    stockRptQuery.LPN_ID = this.PreviousPage.LpnID;
                    stockRptQuery.ISHISTORY = this.PreviousPage.History;
                }
                else
                {
                    stockRptQuery.OWNER_ID = "";
                    stockRptQuery.SKU_ID = "";
                    stockRptQuery.SKU_CLASS_ID = "";
                    stockRptQuery.DESCR = "";
                    stockRptQuery.VENDOR_ID = "";
                    stockRptQuery.LOT_ATTR01 = "";
                    stockRptQuery.LOT_ATTR02 = "";
                    stockRptQuery.LOT_ATTR03 = "";
                    stockRptQuery.LOT_ATTR04 = "";
                    stockRptQuery.LOT_ATTR05 = "";
                    stockRptQuery.LOT_ATTR06 = "";
                    stockRptQuery.LOT_ATTR07 = "";
                    stockRptQuery.LOT_ATTR08 = "";
                    stockRptQuery.LOT_ATTR09 = "";
                    stockRptQuery.LOT_ATTR10 = "";
                    stockRptQuery.EXPIRY_DATE = "";
                    stockRptQuery.PRODUCE_DATE = "";
                    stockRptQuery.RECEIPT_DATE = "";
                    stockRptQuery.EXTERNAL_LOT = "";
                    stockRptQuery.LOT = "";
                    stockRptQuery.LOC_ID = "";
                    stockRptQuery.LPN_ID = "";
                    stockRptQuery.ISHISTORY = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            stockRptQuery.IsGetAll = false;
            stockRptQuery.CurrentPage = "0";
            stockRptQuery.PageSize = this.gvStock.PageSize.ToString();
            stockRptQuery.SortField = "OWNER_ID,SKU_ID,LOT,LOC,LPN_ID";
            stockRptQuery.SortDirection = "ASC";
            this.PagerFooter.PageSize = this.gvStock.PageSize;
            this.SetStockRptQueryEntity(stockRptQuery);
            this.BindData2StockList(stockRptQuery);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                base.InitAnalyticsReportGridview(this.gvStock);
                this.LoadData();
            }
        }

        public void SetStockRptQueryEntity(StockRptQueryEntity stockRptQuery)
        {
            base.PageQueryEntitySession = stockRptQuery;
        }

        public StockRptQuery PreviousPage
        {
            get
            {
                return (StockRptQuery) base.PreviousPage;
            }
        }
    }
}

